package com.example.youjingjing.electronicbalance.service;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;
import android.util.SparseArray;

import com.example.youjingjing.electronicbalance.GeneratedProxy;
import com.example.youjingjing.electronicbalance.javaandroid.DBXException;
import com.example.youjingjing.electronicbalance.javaandroid.DSRESTConnection;
import com.example.youjingjing.electronicbalance.javaandroid.TDataSet;
import com.example.youjingjing.electronicbalance.javaandroid.TJSONObject;
import com.example.youjingjing.electronicbalance.utils.JsonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

/**
 * Created by youjingjing on 2017/8/3.
 */

public class MyIntentService extends IntentService {
    SparseArray<String> array;

    public MyIntentService() {
        super("MyIntentService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        getServiceInfo("123");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    private DSRESTConnection getConnection(String host, int port) {

        DSRESTConnection conn = new DSRESTConnection();

        conn.setHost(host);

        conn.setPort(port);

        return conn;
    }
    public void getServiceInfo(String name){

        DSRESTConnection conn = getConnection("10.0.11.192",8080);

        GeneratedProxy.TPRT_DS_Interface ds = new GeneratedProxy.TPRT_DS_Interface(conn);

        try {
            TDataSet tDataSet = ds.GetUserAuthority(name);

            if(tDataSet.next()) {
                TJSONObject tjsonObject = tDataSet.asJSONObject();

//                        Log.e("run: ", "" +  tjsonObject.toString());

//                         Object userName = tjsonObject.get("UserName").value.getInternalObject();
//                        String authority = tjsonObject.get("UserAuthority").value.toString();

                JSONTokener jsonParser = new JSONTokener(JsonUtils.serialize(tjsonObject));

                JSONObject person = (JSONObject) jsonParser.nextValue();

                JSONArray elements = person.getJSONArray("Elements");

                array = new SparseArray<>();
                for (int i = 0; i < tDataSet.getColumns().size(); i++) {
                    JSONObject value = elements.getJSONObject(i);
                    JSONObject value1 = value.getJSONObject("value");
                    JSONArray elements1 = value1.getJSONArray("Elements");
                    JSONObject jsonObject = elements1.getJSONObject(0);
                    String n = jsonObject.get("value").toString();
                    Log.e( "run: ", "门店号：" +n);
                    array.put(i,n);
                }



            }else{
                Log.d("1", "没有数据 " );
            }
        } catch (DBXException e) {
            Log.e("1", "连接失败 " );
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
